<?php
echo "###\n";
$now_pid= posix_getpid();
$exists=[$now_pid];
echo "\n------------\n";
$i=0;
while($i<3){
    $pid = pcntl_fork();
    //子进程里的返回,不再进行while的pcntl_fork
    if($pid == 0){
        echo posix_getpid()." is a child procress!\n";
        return '';
    }
    echo "exists_".$pid.PHP_EOL;
    print_r($exists);
    if(!in_array($pid,$exists)){
        array_push($exists,$pid);
        echo "now pid:".sprintf("%5d",posix_getpid())."--->fork_return:".sprintf("%5d",$pid)."----->p_".$i++.PHP_EOL;
    }

}
echo "end:";print_r($exists);
exit;
/**
执行结果
###

------------
exists_28408
Array
(
[0] => 28407
)
now pid:28407--->fork_return:28408----->p_0
exists_28409
Array
(
[0] => 28407
[1] => 28408
)
now pid:28407--->fork_return:28409----->p_1
exists_28410
Array
(
[0] => 28407
[1] => 28408
[2] => 28409
)
now pid:28407--->fork_return:28410----->p_2
exists_28411
Array
(
[0] => 28407
[1] => 28408
[2] => 28409
[3] => 28410
)
now pid:28407--->fork_return:28411----->p_3
exists_28412
Array
(
[0] => 28407
[1] => 28408
[2] => 28409
[3] => 28410
[4] => 28411
)
now pid:28407--->fork_return:28412----->p_4
end:Array
(
[0] => 28407
[1] => 28408
[2] => 28409
[3] => 28410
[4] => 28411
[5] => 28412
)


 */





